﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WiMo.Games.Drawables;
using System.Windows.Media.Imaging;
using System.Windows.Resources;

namespace WiMo.Games.GameEngine.WP7S.Drawables
{
    public class SpriteImage : SpriteImageBase
    {
        private Image _image;
        private ITexture _texture;

        public SpriteImage()
        {
            IsVisible = true;
            Origin = Origin.Center;
            Force = Vector2.Zero;
            ColorKey = Color.Transparent;
            IsStatic = false;
            Position = PointF.Origin;
            Mass = 1;
            Torque = 0.0f;
            ImageIndex = 0;
            CollisionShape = CollisionShapes.Rectangle;
        }

        public override void Load(IContentManager contentManager, string name)
        {
            _texture = contentManager.GetTexture(name);

            _image = new Image();
            _image.Source = _texture.TextureInformation as ImageSource;

            CollisionDetectionPoints.Clear();

            if (CollisionShape == CollisionShapes.Sphere)
            {
                var rads = 2.0f * Math.PI / 8.0f;
                var radius = Size.Width / 2;

                for (var idx = 0.0f; idx < 8f; ++idx)
                    CollisionDetectionPoints.Add(new Location(Math.Sin(rads * idx) * radius, Math.Cos(rads * idx) * radius));
            }            
        }

        public override int AddImage(IContentManager contentManager, string name)
        {
            var bitmap = contentManager.GetTexture(name);

            TextureList.Add(bitmap);

            return TextureList.Count - 1;
        }

        public override int ImageIndex
        {
            get { return base.ImageIndex; }
            set
            {
                //So we can refresh based upon the new image.
                var disposable = DrawInformation as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                    DrawInformation = null;
                }

                CollisionDetectionPoints.Clear();

                //The sprite might be a different size.
                if (TextureList.Count > 0 && CollisionShape == CollisionShapes.Sphere)
                {
                    var rads = 2.0f * Math.PI / 8.0f;
                    var radius = Size.Width / 2;

                    for (var idx = 0.0f; idx < 8f; ++idx)
                        CollisionDetectionPoints.Add(new Location(Math.Sin(rads * idx) * radius, Math.Cos(rads * idx) * radius));
                }

                base.ImageIndex = value;
            }
        }

        public ITexture Texture
        {
            get { return TextureList[ImageIndex]; }
        }

        public override Size Size
        {
            get
            {
                var image = _texture.TextureInformation as BitmapImage;

                return new Size(image.PixelWidth * Scale.X, image.PixelHeight * Scale.Y);
            }
            set { throw new Exception("Setting size of bitmap not supported."); }
        }

        public override Size UnscaledSize
        {
            get
            {
                return new Size((int)(Size.Width / Scale.X), (int)(Size.Height / Scale.Y));
            }
            set { throw new Exception("Setting size of bitmap not supported."); }
        }

        public override void Render(IDisplay display)
        {
            DrawInformation = _image;

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scale = new ScaleTransform();
            scale.ScaleX = Scale.X;
            scale.ScaleY = Scale.Y;

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scale);

            _image.RenderTransform = tg;

            if (Origin == WiMo.Games.Drawables.Origin.Center)
            {
                _image.SetValue(Canvas.TopProperty, (double)(Position.Y - (Size.Height / 2)));
                _image.SetValue(Canvas.LeftProperty, (double)(Position.X - (Size.Width / 2)));
            }
            else
            {
                _image.SetValue(Canvas.TopProperty, (double)Position.Y);
                _image.SetValue(Canvas.LeftProperty, (double)Position.X);
            }

            display.Render(this);
        }
    }
}
